package com.yw.common.core.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * IO工具
 * @author aoshiguchen
 * @time 2015-09-09
 */

public final class IOUtil {
	
	public static byte[] inputStreamToByteArray(InputStream in) throws Exception{
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buf=new byte[1024];
		int len=0;
		while((len=in.read(buf))!=-1){
			baos.write(buf,0,len);
		}
		baos.close();
		
		return baos.toByteArray();
	}
	
	public static String inputStreamToString(InputStream in,String charSet) throws Exception{
		String res = "";
		
		byte[] buf = inputStreamToByteArray(in);
		
		if(charSet != null && !charSet.trim().equals("")){
			res = new String(buf,charSet);
		}else{
			res = new String(buf);
		}
		
		return res;
	}
	
	public static String inputStreamToString(InputStream in) throws Exception{
		
		return inputStreamToString(in,null);
	}
	
}
